package project1;

import java.awt.Component;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;





public class GameDemo {
	
	private JFrame frame;
	private BallPanel gamePanel;
	private KeyEvent ke;
	static int firstSize;
	
	public GameDemo(){
		JFrame frame = new JFrame("Ping Pong Game");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(800, 400);
		gamePanel = new BallPanel();
		frame.add(gamePanel);
		frame.addKeyListener(new KeyMovementListener());
		frame.setVisible(true);
	}
	
	public void startGame() {
		
        while (true)
        {
        	try {
				Thread.sleep(200);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
           gamePanel.moveBall();
           gamePanel.drawBall();
           gamePanel.repaint();
           gamePanel.drawPlayer();
           gamePanel.repaint();
           gamePanel.moveComputerUpAndDown();
           gamePanel.drawComputer();
           gamePanel.repaint();
        	
        	
            
        }
		
	}
	
	public static void main(String[] args) {
		GameDemo game = new GameDemo();
		game.startGame();
		

	}
	private class KeyMovementListener implements KeyListener {
		public void keyTyped(KeyEvent e) {}
		public void keyReleased(KeyEvent e) {}

		@Override
		public void keyPressed(KeyEvent e) {
			ke = e;
			if(ke.getKeyCode() == KeyEvent.VK_UP) {
				gamePanel.movePlayerUp();
			} else if(ke.getKeyCode() == KeyEvent.VK_DOWN) {
				gamePanel.movePlayerDown();
			} else if(ke.getKeyCode() == KeyEvent.VK_ESCAPE) {
				frame.dispose();
			}
			
		}
	}


}
